package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"
	_ "user_bff/initialize"
	"user_bff/router"
)

func main() {
	initRouter := router.InitRouter()
	srv := &http.Server{
		Addr:    fmt.Sprintf("%s:%d", "127.0.0.1", 8081),
		Handler: initRouter,
	}
	//优雅的退出
	go func() {
		err := srv.ListenAndServe()
		if err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen %s\n", err)
		}
	}()
	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	timeout, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancelFunc()
	err := srv.Shutdown(timeout)
	if err != nil {
		log.Fatal("Server Shutdown:", err)
	}
}
